#region File Description
//-----------------------------------------------------------------------------
// OptionsMenuScreen.cs
//
// Microsoft XNA Community Game Platform
// Copyright (C) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#endregion

#region Using Statements
using Microsoft.Xna.Framework;
using ShootThemUp.Sources.PlayerInputs.WindowsPhone;
using ShootThemUp.Sources.Configuration;
using ShootThemUp.Sources.AudioElements;
#endregion

namespace ShootThemUp.Sources.Screens
{
    /// <summary>
    /// The options screen is brought up over the top of the main menu
    /// screen, and gives the user a chance to configure the game
    /// in various hopefully useful ways.
    /// </summary>
    class OptionsMenuScreen : MenuScreen
    {
        #region Fields

        /// <summary>
        /// The inputs used on windows phone in order to move the ship (accel or touchscreen).
        /// </summary>
        private MenuEntry movementType;
        /// <summary>
        /// Shot automatically or not.
        /// </summary>
        private MenuEntry autoShot;
        /// <summary>
        /// All songs available.
        /// </summary>
        private MenuEntry song;
        /// <summary>
        /// Play sound effect or not.
        /// </summary>
        private MenuEntry soundEffect;

        /// <summary>
        /// Current movement type used.
        /// </summary>
        private MovementType currentMovementType = PlayerConfiguration.MovementType;        
        private bool currentAutoShot = PlayerConfiguration.AutoShot;
        /// <summary>
        /// The song to play during the game.
        /// </summary>
        private ExtraSong currentSong = PlayerConfiguration.Song;
        private bool currentSoundEffect = PlayerConfiguration.SoundEffect;

        #endregion

        #region Initialization

        /// <summary>
        /// Constructor.
        /// </summary>
        public OptionsMenuScreen()
            : base("\n\n\nOptions")
        {
            // Create our menu entries.
            MenuEntry span = new MenuEntry("", false);
            movementType = new MenuEntry(string.Empty);
            autoShot = new MenuEntry(string.Empty);
            song = new MenuEntry(string.Empty);
            soundEffect = new MenuEntry(string.Empty);

            SetMenuEntryText();

            // Hook up menu event handlers.
            movementType.Selected += MovementTypeMenuEntrySelected;
            autoShot.Selected += AutoShotSelected;
            song.Selected += SongSelected;
            soundEffect.Selected += SoundEffectSelected;

            // Add entries to the menu.
            MenuEntries.Add(span);
            MenuEntries.Add(movementType);
            MenuEntries.Add(autoShot);
            MenuEntries.Add(song);
            MenuEntries.Add(soundEffect);
        }


        /// <summary>
        /// Fills in the latest values for the options screen menu text.
        /// </summary>
        private void SetMenuEntryText()
        {
            movementType.Text = "Controls: " + currentMovementType;
            autoShot.Text = "Auto-Shot: " + (currentAutoShot ? "Yes" : "No");
            song.Text = "Song: " + currentSong;
            soundEffect.Text = "Sound Effect: " + (currentSoundEffect ? "Yes" : "No");
        }

        #endregion

        #region Handle Input

        /// <summary>
        /// Event handler for when the Movement type menu entry is selected.
        /// </summary>
        private void MovementTypeMenuEntrySelected(object sender, PlayerIndexEventArgs e)
        {
            currentMovementType++;

            if (currentMovementType > MovementType.Touchscreen)
                currentMovementType = 0;
            Audio audio = Audio.GetInstance;
            audio.Option_Click.Play();
            SetMenuEntryText();
        }

        /// <summary>
        /// Event handler for when the auto shot menu entry is selected.
        /// </summary>
        /// <param name="sender">The event sender.</param>
        /// <param name="e">The <see cref="ShootThemUp.Sources.Screens.PlayerIndexEventArgs"/> instance containing the event data.</param>
        private void AutoShotSelected(object sender, PlayerIndexEventArgs e)
        {
            Audio audio = Audio.GetInstance;
            audio.Option_Click.Play();
            currentAutoShot = !currentAutoShot;
            SetMenuEntryText();
        }

        /// <summary>
        /// Event handler for when the song menu entry is selected.
        /// </summary>
        /// <param name="sender">The event sender.</param>
        /// <param name="e">The <see cref="ShootThemUp.Sources.Screens.PlayerIndexEventArgs"/> instance containing the event data.</param>
        private void SongSelected(object sender, PlayerIndexEventArgs e)
        {
            currentSong++;

            if (currentSong > ExtraSong.None)
                currentSong = 0;
            Audio audio = Audio.GetInstance;
            audio.Option_Click.Play();
            SetMenuEntryText();
        }

        /// <summary>
        /// Event handler for when the sound effect menu entry is selected.
        /// </summary>
        /// <param name="sender">The event sender.</param>
        /// <param name="e">The <see cref="ShootThemUp.Sources.Screens.PlayerIndexEventArgs"/> instance containing the event data.</param>
        private void SoundEffectSelected(object sender, PlayerIndexEventArgs e)
        {
            Audio audio = Audio.GetInstance;
            audio.Option_Click.Play();
            currentSoundEffect = !currentSoundEffect;
            SetMenuEntryText();
        }

        /// <summary>
        /// Handler for when the user has cancelled the menu. Save the options.
        /// </summary>
        /// <param name="playerIndex"></param>
        protected override void OnCancel(PlayerIndex playerIndex)
        {
            Audio audio = Audio.GetInstance;
            audio.Menu_Click.Play();
            PlayerConfiguration.MovementType = this.currentMovementType;
            PlayerConfiguration.AutoShot = this.currentAutoShot;
            PlayerConfiguration.Song = this.currentSong;
            PlayerConfiguration.SoundEffect = this.currentSoundEffect;
            base.OnCancel(playerIndex);
        }

        #endregion
    }
}
